package com.dayanxs.Controller;

import com.dayanxs.Service.AdminService;
import com.dayanxs.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;


import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/admin")
public class AdminController {

    private final AdminService adminService;

    @Autowired
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    @PostMapping("/login")
    public ResponseEntity<?> login(
            @RequestBody Map<String, String> adminLogin
    ) {
        return adminService.handleLogin(adminLogin.get("username"), adminLogin.get("password"));
    }

    @GetMapping("/system-info")
    public ResponseEntity<?> getSystemInfo(
            @RequestHeader(value = "Authorization", required = false) String authorization
    ) {
        return adminService.systemInfo(authorization);
    }

    @GetMapping("/user/all")
    public ResponseEntity<?> getAllUsers(
            @RequestHeader(value = "Authorization", required = false) String authorization,
            @RequestParam(defaultValue = "10") int size, // 默认每页 10 条
            @RequestParam(defaultValue = "1") int page) {
        return adminService.getUsersWithPagination(authorization,size, page); // 逻辑处理在 Service 层
    }

    @GetMapping("/chapter/all")
    public ResponseEntity<?> getAllChapters(
            @RequestHeader(value = "Authorization", required = false) String authorization,
            @RequestParam(defaultValue = "10") int size, // 默认每页 10 条
            @RequestParam(defaultValue = "1") int page) {
        return adminService.getChaptersWithPagination(authorization,size, page); // 逻辑处理在 Service 层
    }

}
